package com.zlt.service.imlp;

import com.zlt.dao.IUserDao;
import com.zlt.pojo.PageBean;
import com.zlt.pojo.User;
import com.zlt.service.IUserService;
import com.zlt.utils.JWTUtil;
import com.zlt.utils.MD5Util;
import com.zlt.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired(required = false)
    IUserDao userDao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public String login(HttpServletResponse response,String validataCode, String username, String password, String userKey) {
        //1.y验证码是否匹配：frontValidateCode = 找到对应的存储好的验证码（redis）
        String validateCodeRedis = (String) redisUtil.get(userKey);
        if(validateCodeRedis!=null && validateCodeRedis.equals(validataCode)){
            //2.根据用户名查询该用户是否存在
            List<User> list = userDao.findByName(username);
            if(list == null || list.size()==0){
                System.out.print("查询结果：");
                System.out.println(list=null);
                System.out.println("控制台：用户名不存在");
                return "用户名不存在";
            }else {
                //3.验证密码：（frontPassword + salt）---passwordDB
                for(User user:list){
                    String passwordDB = user.getPassword();
                    String salt = user.getSalt();
                    //二次加盐
                    String frontpassword = MD5Util.formPassToDBPass(password,salt);
                    if(passwordDB!=null && passwordDB.equals(frontpassword)){
                        //4.登录成功，发放token跟用户
                        String token = JWTUtil.sign(username,password);
                        response.setHeader("token",token);
                        System.out.println("控制台：登录成功");
                        return "登录成功";
                    }
                }
                System.out.println("密码：" + password);
                System.out.println("控制台：密码错误");
                return "密码错误";
            }

        }else{
            System.out.println("控制台：验证码错误" + "validateCodeRedis:" + validateCodeRedis);
            System.out.println("validataCode:" + validataCode);
            System.out.println(response + "/" + username + "/" + password + "/" + userKey);
            return "验证码错误";
        }
    }

    @Override
    public String add(User user) {
        userDao.add(user);
        return "ok";
    }

    @Override
    public String del(int id) {
        userDao.del(id);
        return "ok";
    }

    @Override
    public String edit(User user) {
        userDao.edit(user);
        return "ok";
    }

    @Override
    public User select(int id) {

        return userDao.select(id);
    }

    @Override
    public PageBean<User> pageBreak(String pageNum) {
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setPageNum(Integer.parseInt(pageNum));           //设置当前页码
        int totalCount = userDao.getTotal();                     //获取总共有多少条数据
        pageBean.setPageCount(totalCount);                       //设置总共有多少页
        pageBean.setData(userDao.findByPage(                       //设置返回的数据
                (Integer.parseInt(pageNum)-1)*10        //传入参数时需要先将运算完成
        ));
        return pageBean;
    }
}
